package org.huangrui.juc.source.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * @Author hr
 * @Create 2024-04-02 23:33
 */
public class T01_VolatileSee {
    // static boolean flag = true;          //不加volatile，没有可见性
    static volatile boolean flag = true;    //加了volatile，保证可见性
    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----come in");
            while(flag){
            }
            System.out.println(Thread.currentThread().getName()+"\t -----flag被设置为false，程序停止");
        },"t1").start();

        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
        flag = false;
        System.out.println(Thread.currentThread().getName()+"\t 修改完成flag: "+flag);

    }
}
